package com.matt.service.impl;

import com.google.common.collect.ImmutableMap;
import com.matt.domain.course.CoursePlanClasses;
import com.matt.domain.record.LiveRecord;
import com.matt.domain.rel.RelTeacherCoursePlanHistory;
import com.matt.foundation.DO.TeacherDO;
import com.matt.foundation.constant.LiveRecordConstant;
import com.matt.foundation.vo.ClassesListVo;
import com.matt.repository.base.ClassesTeacherRepository;
import com.matt.repository.course.CoursePlanClassesRepository;
import com.matt.repository.record.LiveRecordRepository;
import com.matt.repository.rel.RelClassesTeacherRepository;
import com.matt.repository.rel.RelTeacherCoursePlanRepository;
import com.matt.service.TeacherService;
import com.matt.service.remoteCall.SsoCallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/7/11
 * *******************************
 */
@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private ClassesTeacherRepository classesTeacherRepository;

    @Autowired
    private LiveRecordRepository liveRecordRepository;

    @Autowired
    private SsoCallService ssoCallService;

    @Autowired
    private CoursePlanClassesRepository coursePlanClassesRepository;

    @Autowired
    private RelClassesTeacherRepository relClassesTeacherRepository;

    @Autowired
    private RelTeacherCoursePlanRepository relTeacherCoursePlanRepository;

//    @Override
//    public Map getBaseInfo(String staffId, String areaId) {
//        StaffDTO staff = ssoCallService.getStaffDTO(null, staffId);
//        AreaDTO area = ssoCallService.getAreaById(areaId);
//        Map response = new HashMap();
//        response.put("id", staff.getId());
//        response.put("name", staff.getRealName());
//        response.put("imgUrl", staff.getHeadImgUrl());
//        response.put("area", area.getName());
//        return response;
//    }


    @Override
    public List<ClassesListVo> classesList(String staffId, String areaId) {
        List<Object[]> objects = classesTeacherRepository.findByUserAndArea(staffId, areaId);
        List<ClassesListVo> classesList = objects.stream().map(o -> ClassesListVo.of(o)).collect(Collectors.toList());
        return classesList;
    }

//    @Override
//    public List<StaffAreaDTO> getTeacherAndGardenLeaderByArea(String areaId) {
//        return ssoCallService.getStaffByPlatformAndRoleAndArea(CommonConstant.TEACH_PLATFORM_ID, new String[]{PermissionConstant.Role.TEACHER.codeOf(), PermissionConstant.Role.GARDEN_LEADER.codeOf()}, areaId);
//    }

    @Override
    public Map getMediaStatisticsInfo(String staffId) {
        List<LiveRecord> liveRecords = liveRecordRepository.findByOperatorIdAndDeleted(staffId, false);
        Integer imageNum = 0;
        Integer videoNum = 0;
        for (LiveRecord o : liveRecords) {
            if (LiveRecordConstant.MediaType.IMG.codeOf() == o.getMediaType())
                imageNum++;
            else
                videoNum++;
        }
        return ImmutableMap.of("imageNum", imageNum, "videoNum", videoNum, "recordNum", imageNum + videoNum);
    }

    @Override
    public List<TeacherDO> getTeacherByClasses(String classesId) {
        List<TeacherDO> teacherList = classesTeacherRepository.findByClassesId(classesId);
        return teacherList;
    }

    @Override
    public List<String> findStaffIdsByChildId(String childrenId) {
        return classesTeacherRepository.findStaffIdsByChildId(childrenId);
    }

    @Transactional
    @Override
    public Boolean precessCourse(String coursePlanClassesId) {
        CoursePlanClasses coursePlanClasses = coursePlanClassesRepository.getOne(coursePlanClassesId);
        List<String> teachers = relClassesTeacherRepository.findByClassesId(coursePlanClasses.getClassesId()).stream().map(o->o.getStaffId()).collect(Collectors.toList());

        List<RelTeacherCoursePlanHistory> batch = new ArrayList<>();
        teachers.forEach(o->{
            batch.add(RelTeacherCoursePlanHistory.of(o, coursePlanClassesId));
        });

        return relTeacherCoursePlanRepository.saveAll(batch).size() == teachers.size();
    }
}
